package zhtt.biz01.web.config;

import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

@Component
public class RedisUtil {

    private RedisTemplate template;

    @Bean
    private RedisUtil util(RedisTemplate template) {
        return new RedisUtil(template);
    }

    public RedisUtil(RedisTemplate template) {
        this.template = template;
    }

    public boolean setValue(Object key, Object value){
        try{
            template.opsForValue().set(key, value);
            return true;
        }catch(Exception e){
            e.printStackTrace();
            return false;
        }
    }

    public Object getValue(Object key){
        return template.opsForValue().get(key);
    }

    public Map<String,Object> getAllConfig(){
        Set<String> keys =template.keys("*");
        Iterator<String> it1 = keys.iterator();
        Map<String,Object> map = new HashMap<String,Object>();
        while (it1.hasNext()) {
            String key = it1.next();
            map.put(key,getValue(key));
        }
        return map;
    }
}
